﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportService2Ref;
    using WMS6.Web.UserControls;

    public class HistoryStockRptList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15286;
        protected GridView gvStock;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnInvDate;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected Label lblTotalCube;
        protected Label lblTotalCubeText;
        protected Label lblTotalGrossWgt;
        protected Label lblTotalGrossWgtText;
        protected Label lblTotalQty;
        protected Label lblTotalQtyText;
        protected LinkButton lnkbtnInvBakeUp;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnInvBakeUp;
        private decimal TotalCube;
        private decimal TotalGrossWgt;
        private decimal TotalQty;
        protected UpdatePanel up1;

        public void BindData2StockList(HistoryStockRptQueryEntity historyStockRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(historyStockRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetHistoryStockRptDataSet(historyStockRptQueryEntity);
            base.BindData2GridView(this.gvStock, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            HistoryStockRptQueryEntity historyStockRptQueryEntity = this.GetHistoryStockRptQueryEntity();
            historyStockRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = historyStockRptQueryEntity.CurrentPage;
            historyStockRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetHistoryStockRptDataSet(historyStockRptQueryEntity))
                {
                    base.Export2Excel("HistoryStockRpt.xls", "15286", this.gvStock, ds);
                }
            }
            finally
            {
                historyStockRptQueryEntity.IsGetAll = false;
                historyStockRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetHistoryStockRptQueryEntity(historyStockRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            HistoryStockRptQueryEntity historyStockRptQueryEntity = this.GetHistoryStockRptQueryEntity();
            historyStockRptQueryEntity.CurrentPage = "0";
            this.BindData2StockList(historyStockRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            HistoryStockRptQueryEntity historyStockRptQueryEntity = this.GetHistoryStockRptQueryEntity();
            historyStockRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2StockList(historyStockRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            HistoryStockRptQueryEntity historyStockRptQueryEntity = this.GetHistoryStockRptQueryEntity();
            historyStockRptQueryEntity.OWNER_ID = "";
            historyStockRptQueryEntity.SKU_ID = "";
            historyStockRptQueryEntity.SKU_CLASS_ID = "";
            historyStockRptQueryEntity.DESCR = "";
            historyStockRptQueryEntity.VENDOR_ID = "";
            historyStockRptQueryEntity.EXTERNAL_LOT = "";
            historyStockRptQueryEntity.LOT = "";
            historyStockRptQueryEntity.LOC_ID = "";
            historyStockRptQueryEntity.LPN_ID = "";
            historyStockRptQueryEntity.INV_DATE = DateTime.Today.AddDays(1.0).ToString("yyyy-MM-dd");
            historyStockRptQueryEntity.LOT_ATTR01 = "";
            historyStockRptQueryEntity.LOT_ATTR02 = "";
            historyStockRptQueryEntity.LOT_ATTR03 = "";
            historyStockRptQueryEntity.LOT_ATTR04 = "";
            historyStockRptQueryEntity.LOT_ATTR05 = "";
            historyStockRptQueryEntity.LOT_ATTR06 = "";
            historyStockRptQueryEntity.LOT_ATTR07 = "";
            historyStockRptQueryEntity.LOT_ATTR08 = "";
            historyStockRptQueryEntity.LOT_ATTR09 = "";
            historyStockRptQueryEntity.LOT_ATTR10 = "";
            historyStockRptQueryEntity.EXPIRY_DATE = "";
            historyStockRptQueryEntity.PRODUCE_DATE = "";
            historyStockRptQueryEntity.RECEIPT_DATE = "";
            this.BindData2StockList(historyStockRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            HistoryStockRptQueryEntity historyStockRptQueryEntity = this.GetHistoryStockRptQueryEntity();
            if (int.Parse(historyStockRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                historyStockRptQueryEntity.CurrentPage = (int.Parse(historyStockRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2StockList(historyStockRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            HistoryStockRptQueryEntity historyStockRptQueryEntity = this.GetHistoryStockRptQueryEntity();
            if (int.Parse(historyStockRptQueryEntity.CurrentPage) > 0)
            {
                historyStockRptQueryEntity.CurrentPage = (int.Parse(historyStockRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2StockList(historyStockRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            HistoryStockRptQueryEntity historyStockRptQueryEntity = this.GetHistoryStockRptQueryEntity();
            this.BindData2StockList(historyStockRptQueryEntity);
        }

        private HistoryStockRptQueryEntity GetHistoryStockRptQueryEntity()
        {
            return (base.PageQueryEntitySession as HistoryStockRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                HistoryStockRptQueryEntity historyStockRptQueryEntity = this.GetHistoryStockRptQueryEntity();
                historyStockRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2StockList(historyStockRptQueryEntity);
            }
        }

        protected void gvStock_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15286", this.gvStock);
        }

        protected void gvStock_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (this.hdnActionID.Value != "EXPORT")
                {
                    e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                    e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                    ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                    e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                    if (((Label) e.Row.FindControl("lblQty")) != null)
                    {
                        this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblQty")).Text != "") ? ((Label) e.Row.FindControl("lblQty")).Text : "0");
                    }
                    if (((Label) e.Row.FindControl("lblGrossWgt")) != null)
                    {
                        this.TotalGrossWgt += decimal.Parse((((Label) e.Row.FindControl("lblGrossWgt")).Text != "") ? ((Label) e.Row.FindControl("lblGrossWgt")).Text : "0");
                    }
                    if (((Label) e.Row.FindControl("lblCube")) != null)
                    {
                        this.TotalCube += decimal.Parse((((Label) e.Row.FindControl("lblCube")).Text != "") ? ((Label) e.Row.FindControl("lblCube")).Text : "0");
                    }
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalQty);
                this.lblTotalGrossWgtText.Text = base.GetPreferenceDigit(this.TotalGrossWgt);
                this.lblTotalCubeText.Text = base.GetPreferenceDigit(this.TotalCube);
                if (this.hdnActionID.Value == "EXPORT")
                {
                    for (int i = 0; i < this.gvStock.Columns.Count; i++)
                    {
                        if (this.gvStock.Columns[i].Visible)
                        {
                            this.gvStock.Columns[i].ItemStyle.Reset();
                            this.gvStock.Columns[i].ControlStyle.Reset();
                        }
                    }
                }
            }
        }

        protected void gvStock_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                HistoryStockRptQueryEntity historyStockRptQueryEntity = this.GetHistoryStockRptQueryEntity();
                historyStockRptQueryEntity.SortField = e.SortExpression;
                if ((historyStockRptQueryEntity.SortDirection == "ASC") || (historyStockRptQueryEntity.SortDirection == ""))
                {
                    historyStockRptQueryEntity.SortDirection = "DESC";
                }
                else if (historyStockRptQueryEntity.SortDirection == "DESC")
                {
                    historyStockRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2StockList(historyStockRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnInvBakeUp_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.hdnInvDate.Value))
                {
                    this.lblMsg.Text = "库存日期不能为空！";
                }
                else
                {
                    string invDate = Convert.ToDateTime(this.hdnInvDate.Value).ToString("yyyy-MM-dd");
                    if (!string.IsNullOrEmpty(InventoryMgtServiceClientProxy.GetInstance().InsertInvBackup(invDate)))
                    {
                        this.lblMsg.Text = "库存快照成功！";
                    }
                    else
                    {
                        this.lblMsg.Text = "库存快照失败！";
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        private void LoadData()
        {
            HistoryStockRptQueryEntity historyStockRptQueryEntity = new HistoryStockRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    historyStockRptQueryEntity.OWNER_ID = this.PreviousPage.OwnerID;
                    historyStockRptQueryEntity.SKU_ID = this.PreviousPage.SkuID;
                    historyStockRptQueryEntity.SKU_CLASS_ID = this.PreviousPage.SkuClassID;
                    historyStockRptQueryEntity.DESCR = this.PreviousPage.Descr;
                    historyStockRptQueryEntity.VENDOR_ID = this.PreviousPage.VendorID;
                    historyStockRptQueryEntity.EXTERNAL_LOT = this.PreviousPage.ExternalLot;
                    historyStockRptQueryEntity.LOT = this.PreviousPage.Lot;
                    historyStockRptQueryEntity.LOC_ID = this.PreviousPage.LocationID;
                    historyStockRptQueryEntity.LPN_ID = this.PreviousPage.LpnID;
                    historyStockRptQueryEntity.INV_DATE = this.PreviousPage.InvDate;
                    historyStockRptQueryEntity.LOT_ATTR01 = this.PreviousPage.LotAttribute01;
                    historyStockRptQueryEntity.LOT_ATTR02 = this.PreviousPage.LotAttribute02;
                    historyStockRptQueryEntity.LOT_ATTR03 = this.PreviousPage.LotAttribute03;
                    historyStockRptQueryEntity.LOT_ATTR04 = this.PreviousPage.LotAttribute04;
                    historyStockRptQueryEntity.LOT_ATTR05 = this.PreviousPage.LotAttribute05;
                    historyStockRptQueryEntity.LOT_ATTR06 = this.PreviousPage.LotAttribute06;
                    historyStockRptQueryEntity.LOT_ATTR07 = this.PreviousPage.LotAttribute07;
                    historyStockRptQueryEntity.LOT_ATTR08 = this.PreviousPage.LotAttribute08;
                    historyStockRptQueryEntity.LOT_ATTR09 = this.PreviousPage.LotAttribute09;
                    historyStockRptQueryEntity.LOT_ATTR10 = this.PreviousPage.LotAttribute10;
                    historyStockRptQueryEntity.EXPIRY_DATE = this.PreviousPage.ExpiryDate;
                    historyStockRptQueryEntity.PRODUCE_DATE = this.PreviousPage.ProduceDate;
                    historyStockRptQueryEntity.RECEIPT_DATE = this.PreviousPage.ReceiptDate;
                }
                else
                {
                    historyStockRptQueryEntity.OWNER_ID = "";
                    historyStockRptQueryEntity.SKU_ID = "";
                    historyStockRptQueryEntity.SKU_CLASS_ID = "";
                    historyStockRptQueryEntity.DESCR = "";
                    historyStockRptQueryEntity.VENDOR_ID = "";
                    historyStockRptQueryEntity.EXTERNAL_LOT = "";
                    historyStockRptQueryEntity.LOT = "";
                    historyStockRptQueryEntity.LOC_ID = "";
                    historyStockRptQueryEntity.LPN_ID = "";
                    historyStockRptQueryEntity.INV_DATE = DateTime.Today.AddDays(1.0).ToString("yyyy-MM-dd");
                    historyStockRptQueryEntity.LOT_ATTR01 = "";
                    historyStockRptQueryEntity.LOT_ATTR02 = "";
                    historyStockRptQueryEntity.LOT_ATTR03 = "";
                    historyStockRptQueryEntity.LOT_ATTR04 = "";
                    historyStockRptQueryEntity.LOT_ATTR05 = "";
                    historyStockRptQueryEntity.LOT_ATTR06 = "";
                    historyStockRptQueryEntity.LOT_ATTR07 = "";
                    historyStockRptQueryEntity.LOT_ATTR08 = "";
                    historyStockRptQueryEntity.LOT_ATTR09 = "";
                    historyStockRptQueryEntity.LOT_ATTR10 = "";
                    historyStockRptQueryEntity.EXPIRY_DATE = "";
                    historyStockRptQueryEntity.PRODUCE_DATE = "";
                    historyStockRptQueryEntity.RECEIPT_DATE = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            this.hdnInvDate.Value = historyStockRptQueryEntity.INV_DATE;
            historyStockRptQueryEntity.IsGetAll = false;
            historyStockRptQueryEntity.CurrentPage = "0";
            historyStockRptQueryEntity.PageSize = this.gvStock.PageSize.ToString();
            historyStockRptQueryEntity.SortField = "OWNER_ID,SKU_ID,LOT,LOC,LPN_ID";
            historyStockRptQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvStock.PageSize;
            this.SetHistoryStockRptQueryEntity(historyStockRptQueryEntity);
            this.BindData2StockList(historyStockRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvStock);
                this.LoadData();
            }
        }

        public void SetHistoryStockRptQueryEntity(HistoryStockRptQueryEntity historyStockRptQueryEntity)
        {
            base.PageQueryEntitySession = historyStockRptQueryEntity;
        }

        public HistoryStockRptQuery PreviousPage
        {
            get
            {
                return (HistoryStockRptQuery) base.PreviousPage;
            }
        }
    }
}

